package gui;


import javax.swing.*;
import javax.swing.table.DefaultTableColumnModel;
import javax.swing.table.TableColumn;
import javax.swing.table.TableColumnModel;
import javax.swing.table.TableModel;
import javax.swing.text.TableView;

import data.DataRecord;
import data.DataSet;

import java.awt.*;
import java.awt.event.*;
import java.util.ArrayList;
import logic.InterconFacade;

public class TableGUI {
	
	private class ButtonListener implements ActionListener {
		private JButton button;
		public ButtonListener( JButton button ) {
			super();
			this.button = button;
		}
		@SuppressWarnings("deprecation")
		@Override
		public void actionPerformed(ActionEvent arg0) {
			tablemodel = intercon.getAnalyzeByInterviewer( button.getLabel() );
			table.setModel( tablemodel );
			topText.setText( intercon.analyzeTotaltoString());
		}
	}



	InterconFacade intercon;
	JTable table;
	TableModel tablemodel;
	private DefaultTableColumnModel columns;
	private JTextArea topText;

	public void start( InterconFacade intercon ) {
		
		this.intercon = intercon;
		tablemodel = new DataSet();
		table = new JTable( tablemodel );
		

		topText = new JTextArea();
		topText.setLineWrap( true );
		JScrollPane topScroller = new JScrollPane( topText );
			topScroller.setVerticalScrollBarPolicy( 
				ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS );
			topScroller.setHorizontalScrollBarPolicy(
				ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER );
	
		JScrollPane scroller = new JScrollPane( table );
			scroller.setVerticalScrollBarPolicy( 
				ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS );
			scroller.setHorizontalScrollBarPolicy(
				ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER );
		ArrayList<String> projects = intercon.getProjects();

		JPanel panel = new JPanel();		
		panel.setLayout( new BoxLayout( panel, BoxLayout.Y_AXIS ));
		panel.add( topScroller );
		panel.add( scroller );	
		for ( String name : projects ) {
		JButton button = new JButton( name );
			button.addActionListener( new ButtonListener( button ));	
			panel.add( button );
		}
			
		JFrame frame = new JFrame();
			frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
			frame.add( panel );
			frame.setSize( 1000, 800 );
			frame.setVisible( true );
	}

}